package interfaces.superWidgets;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.BorderLayout;
import org.fenggui.layout.BorderLayoutData;
import org.fenggui.util.Color;

import fileHandling.language.Lang;

import interfaces.GUISource;
import interfaces.hud.BasicHUD;
import interfaces.language.LanguageButton;

public class MinimizeWindow extends InterfaceWindow {
	
	public enum Mode { UP, DOWN, BUTTONDOWN }
	
	protected LanguageButton minimizeButton;
	protected InterfaceContainer content;
	protected boolean isMinimized;
	protected int oldHeight, oldY;
	protected Mode mode;
	protected BasicHUD hud;
	protected ImageFont minimizeButtonFont;
	
	public MinimizeWindow(Lang title, Mode mode, BasicHUD hud) {
		super(false, "");
		this.mode = mode;
		this.hud = hud;
		
		super.content.setLayoutManager(new BorderLayout());
		
		minimizeButtonFont = GUISource.middleFont;
		minimizeButton = new LanguageButton(title, minimizeButtonFont);
		minimizeButton.addButtonPressedListener(getButtonListener());
		
		if(mode == Mode.BUTTONDOWN) minimizeButton.setLayoutData(BorderLayoutData.SOUTH);
		else minimizeButton.setLayoutData(BorderLayoutData.NORTH);
		
		minimizeButton.getAppearance().add(new PlainBackground(Color.BLUE));
		GUISource.setTextColor(minimizeButton, Color.WHITE);
		super.content.addWidget(minimizeButton);
		
		content = new InterfaceContainer();
		content.setLayoutData(BorderLayoutData.CENTER);
		super.content.addWidget(content);
	}
	
	private IButtonPressedListener getButtonListener() {
		return new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				setMinimized(!isMinimized);
				e.getTrigger().setFocused(false);
			}
		};
	}
	
	public void setMinimized(boolean minimize) {
		if(minimize == isMinimized) return;
		if(!minimize) {
			super.content.addWidget(content);
			setHeight(oldHeight);
		} else {
			oldHeight = getHeight();
			super.content.removeWidget(content);
			setHeight(minimizeButton.getHeight() + getAppearance().getBorder().getBottom() * 2);
		}
		if(mode == Mode.UP) setY(hud.getHeight() - getHeight());
		isMinimized = minimize;
		layout();
	}
}